
package com.hc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hc.dao.IRoleAndNodesDao;
import com.hc.domain.Nodes;
import com.hc.domain.Role;
import com.hc.domain.RoleAndNodes;
import com.hc.service.INodesService;
import com.hc.service.IRoleAndNodesService;
import com.hc.service.IRoleService;
import com.hc.util.LoggerUtil;
import com.hc.util.TaskThreadProvider;

@Controller
@RequestMapping("Node")
public class NodeController extends BaseController {
	@Resource
	private INodesService nodesService;
	@Resource
	private TaskThreadProvider taskProvider;
	@Resource
	private IRoleService roleService;
	@Resource
	private IRoleAndNodesService roleAndNodesService;

	/**
	 * 
	 * @Description： 通过父节点id获取数据
	 * 
	 * @param req
	 * @param resp
	 * @param parentId
	 *            父节点id
	 * @return
	 * @author:
	 * @since: 2017年6月21日 上午11:59:44
	 */
	@ResponseBody
	@RequestMapping("/getNodesByParentId")
	public JSON getNodesByParentId(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "id", required = true) int id) {
		List<Nodes> ls = nodesService.getNodesByParentId(id);
		if (ls == null || ls.size() == 0) {
			return Message(NO_MORE_INFO, "没有数据", null);
		}
		return Message(SUCCESS, "获取数据成功", ls);
	}

	/**
	 * 
	 * @Description： 添加一个节点
	 * 
	 * @param req
	 * @param resp
	 * @param nodeName
	 *            节点名
	 * @param isCheck
	 *            是否存在子节点 true：存在
	 * @param nodePath
	 *            跳转路径
	 * @param parentId
	 *            父节点id 如果已经是父节点 则为0
	 * @param icon
	 *            icon 图标
	 * @return
	 * @author:
	 * @since: 2017年6月21日 下午4:59:26
	 */
	@ResponseBody
	@RequestMapping(value = "/addNode", method = RequestMethod.POST)
	public JSON addNode(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "nodeName", required = true) String nodeName,
			@RequestParam(value = "isCheck", required = true) Boolean isCheck,
			@RequestParam(value = "nodePath", required = false) String nodePath,
			@RequestParam(value = "parentId", required = true) int parentId,
			@RequestParam(value = "icon", required = false) String icon) {
		String trimName = nodeName.trim();
		if (isEmpty(trimName)) {
			return Message(ERROR, "节点名称不能为空", null);
		}
		Nodes n = null;
		if (parentId != 0) {
			Nodes node = nodesService.getNodesById(parentId);
			if (node.getStatus() == 1) {
				return Message(ERROR, "该节店不存在子节点，无法添加", null);
			}
			String trimPath = nodePath.trim();
			if (isEmpty(trimPath)) {
				return Message(ERROR, "节点路径不能为空", null);
			}
			n = new Nodes();
			n.setIcon(icon);
			n.setNode(trimName);
			n.setParentId(parentId);
			n.setPath(trimPath);
			n.setStatus(1);
		}

		if (parentId == 0 && isCheck) {
			n = new Nodes();
			n.setIcon(icon);
			n.setParentId(0);
			n.setPath("");
			n.setNode(trimName);
			n.setStatus(0);
		}
		if (parentId == 0 && !isCheck) {
			String trimPath = nodePath.trim();
			if (isEmpty(trimPath)) {
				return Message(ERROR, "节点路径不能为空", null);
			}
			n = new Nodes();
			n.setIcon(icon);
			n.setParentId(0);
			n.setPath(trimPath);
			n.setNode(trimName);
			n.setStatus(1);
		}
		nodesService.updateOrSave(n);
		return Message(SUCCESS, "操作成功", n);
	}

	/**
	 * 
	 * @Description： 删除单个节点
	 * 
	 * @param req
	 * @param resp
	 * @param id
	 * @return
	 * @author:
	 * @since: 2017年6月21日 下午5:48:00
	 */
	@ResponseBody
	@RequestMapping(value = "/delNode", method = RequestMethod.POST)
	public JSON delNode(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "id", required = true) int id) {
		Nodes n = nodesService.getNodesById(id);
		if (n == null) {
			return Message(ERROR, "该节点已删除", null);
		}
		if (n.getStatus() == 0) {
			List<Nodes> nodes = nodesService.getNodesByParentId(id);
			if (nodes != null && nodes.size() > 0) {
				return Message(ERROR, "该节点存在子节点，无法删除", null);
			}
		}
		nodesService.deleteNodeAndAuth(n);
		return Message(SUCCESS, "删除成功", null);
	}

	/**
	 * 
	 * @Description： 获取对应角色有权限的菜单节点Id
	 * 
	 * @param req
	 * @param resp
	 * @param roleId
	 *            角色Id
	 * @return
	 * @author:
	 * @since: 2017年6月22日 下午6:12:12
	 */
	@ResponseBody
	@RequestMapping(value = "/getAuthNodes", method = RequestMethod.POST)
	public JSON getAllNodes(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "roleId", required = true) int roleId) {
		Role role = roleService.getRoleById(roleId);
		if (role == null) {
			return Message(ERROR, "当前角色不存在，或已删除", null);
		}
		List<Nodes> nodes = nodesService.getNodesByRoleId(roleId);
		JSONArray nodeIds = new JSONArray();
		if (nodes != null && nodes.size() > 0) {
			for (Nodes n : nodes) {
				nodeIds.add(n.getId());
			}
			return Message(SUCCESS, "获取数据成功", nodeIds);
		} else {
			return Message(NO_MORE_INFO, "获取数据成功", nodeIds);
		}
	}
	/**
	 * 
	 * @Description： 授权以及撤权
	 * @param req
	 * @param resp  
	 * @param roleId 角色id
	 * @param nodeId 节点id
	 * @param status 1：授权  2：撤权
	 * @return
	 * @author:  
	 * @since: 2017年6月22日 下午6:50:17
	 */
	@ResponseBody
	@RequestMapping(value = "/addAuth", method = RequestMethod.POST)
	public JSON addAuth(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(value = "roleId", required = true) int roleId,
			@RequestParam(value = "nodeId", required = true) int nodeId,
			@RequestParam(value = "status", required = true) int status) {
		Nodes n = nodesService.getNodesById(nodeId);
		if (n == null) {
			return Message(ERROR, "该节点已删除", null);
		}
		if (status==1){
			RoleAndNodes rn = roleAndNodesService.getRoleAndNodeByRoleIdAndNodeId(roleId, nodeId);
			if (rn != null) {
				return Message(ERROR, "该节点已授权，请勿重复授权", null);
			}
			Role role = roleService.getRoleById(roleId);
			if (role == null) {
				return Message(ERROR, "该角色已删除", null);
			}
			if (n.getStatus() == 1 && n.getParentId() != 0) {
				rn = roleAndNodesService.getRoleAndNodeByRoleIdAndNodeId(roleId, n.getParentId());
				if (rn == null) {
					return Message(ERROR, "请先对该节点父节点授权", null);
				}
			}
			rn = new RoleAndNodes();
			rn.setNodeId(nodeId);
			rn.setRoleId(roleId);
			roleAndNodesService.updateOrSave(rn);
			return Message(SUCCESS, "授权成功", null);
		}else if (status==2){
			RoleAndNodes rn = roleAndNodesService.getRoleAndNodeByRoleIdAndNodeId(roleId, nodeId);
			if (rn == null) {
				return Message(ERROR, "该节点已取消授权，请勿重复取消", null);
			}
			if (n.getStatus() == 0 && n.getParentId() == 0) {
				roleAndNodesService.deleteRoleAndNodesAndChlidNode(n);
			}else{
				roleAndNodesService.deleteRoleAndNodes(rn);
			}
			return Message(SUCCESS, "取消授权成功", null);
		}else{
			return Message(ERROR, "未知操作", null);
		}
	}

}
